package com.billdowney.qq.robot.systemTask;

//import com.ruoyi.web.robot.service.RobotService;

import com.billdowney.qq.robot.bean.Qqzhanghao;
import com.billdowney.qq.robot.mapper.QqzhanghaoMapper;
import com.billdowney.qq.robot.service.RobotService;
import com.billdowney.qq.robot.util.IpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 机器人登录初始化所有qq机器人
 * */
@Component
@RestController
//@EnableScheduling
public class ApplicationInitService implements ApplicationRunner {

    @Resource
    private RobotService robotService;
    @Autowired
    QqzhanghaoMapper qqzhanghaoMapper;
//    @Override
//    @Scheduled(cron = "0/5 * * * * ?")//每天凌晨3点执行一次：0 0 3 * * ?
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("qq机器人正在登录。。。。。。。");
        Qqzhanghao qq1 =new Qqzhanghao();
        qq1.setShanchu(0L);
        qq1.setIp(IpUtil.getLocalIp4Address().get().toString().replaceAll("/",""));
        System.out.println("服务器IP:"+IpUtil.getLocalIp4Address().get().toString().replaceAll("/",""));
        List<Qqzhanghao> qqzhanghaos = qqzhanghaoMapper.selectQqzhanghaoList(qq1);
        for (Qqzhanghao qq:qqzhanghaos){
           try{
               Thread qqRunThread = new Thread(() -> {
                   robotService.start(qq.getQq(), qq.getMima());
               });
               qqRunThread.setDaemon(true);
               qqRunThread.setName("QQ机器人服务运行线程:" + qq);
               qqRunThread.start();

           }catch (Exception e){
               e.getMessage();
           }
       }
        System.out.println("qq机器人登录成功..........");
    }




}
